package com.huajx.tcpserver.kit;

import java.util.Arrays;

public abstract class MessageKit {
//	private static final Logger logger = LoggerFactory.getLogger(MessageKit.class);
	
	public static String getStr(byte[] bytes, int start, int len){
		int bLen = bytes.length;
		
		int to = start + len;
		
		if(bLen<to){
			to = bLen;
		}
		
		return bytesToString(Arrays.copyOfRange(bytes, start, to));
	}

	public static int getInt(byte[] bytes, int start, int len){
		int bLen = bytes.length;
		
		int to = start + len;
		
		if(bLen<to){
			to = bLen;
		}
		
		return bytes2Int(Arrays.copyOfRange(bytes, start, to));
	}
	
	// byte数组长度为4, bytes[3]为高8位
	public static int bytes2Int(byte[] bytes) {
		int value = 0;
//		String str = "0x"+bytesToString(bytes);
		value =  Integer.parseInt(bytesToString(bytes),16);
//		value = ((bytes[3] & 0xff) << 24) | ((bytes[2] & 0xff) << 16) | ((bytes[1] & 0xff) << 8) | (bytes[0] & 0xff);
		return value;
	}
	
	public static String bytesToString(byte[] bytes){
		StringBuilder sb = new StringBuilder(bytes.length);
		
		for (int i = 0; i < bytes.length; i++) {
			sb.append((char) bytes[i]); // 可以根据需要自己改变类型
		}
		
//		logger.info(sb.toString());
		
		return sb.toString();
	}
}
